package com.dkm.ec.home.domain;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.core.context.OnlineUserIdHolder;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.domain.GlText;
import com.dkm.biz.gl.persistence.entity.GlTextPo;
import com.dkm.biz.gl.repository.GlTextRepository;
import com.dkm.ec.home.persistence.dao.HomeAnswerDao;
import com.dkm.ec.home.persistence.entity.HomeAnswerPo;
import com.dkm.ec.home.repository.HomeAnswerRepository;

/**
 * 对象功能:skg_home_answer 领域对象实体
 * 开发公司:SKG.COM
 * 开发人员:喻永淼
 * 创建时间:2015-01-07 10:39:31
 */
@Service
@Scope("prototype")
public class HomeAnswer extends AbstractDomain<String, HomeAnswerPo>{
	 
	 private HomeAnswerDao homeAnswerDao = null;
	 private GlTextRepository glTextRepository = null;
	 private HomeAnswerRepository homeAnswerRepository;

	protected void init(){
		homeAnswerDao = SpringHelper.getBean(HomeAnswerDao.class);
		glTextRepository = SpringHelper.getBean(GlTextRepository.class);
		homeAnswerRepository = SpringHelper.getBean(HomeAnswerRepository.class);

		setDao(homeAnswerDao);
	}	 
	 
	
	/**
	 * 专供后台录入保存使用
	 */
	public void save() {
		String answerFid = getData().getAnswerFid();
		
		if(StringUtils.isNotEmpty(answerFid)){
			GlText glText = glTextRepository.load(answerFid);
		//	glText.saveText(getData().getAnswer());
		}else {
			GlText glText = glTextRepository.newInstance();
		//	answerFid = glText.saveText(getData().getAnswer());			
		}		
		getData().setAnswerFid(answerFid);
		getData().setAUserId(OnlineUserIdHolder.getUserId());		
		
		HomeAnswerPo homeAnswerPoTemp=getData();
		
		
		if(getData().getIsBest()=='Y'){
			List<HomeAnswerPo> homeAnswerPos= homeAnswerRepository.findByQaId(getData().getQaId());
			for(HomeAnswerPo homeAnswerPo:homeAnswerPos){
				homeAnswerPo.setIsBest('N');
				 setData(homeAnswerPo);
				 super.save();		
			}
		}
		 setData(homeAnswerPoTemp);
		super.save();		
	}
	
	public void loadRels(){
		if(StringUtils.isNotEmpty(getData().getAnswerFid())){
			GlText glText = glTextRepository.load(getData().getAnswerFid());
		/*	if(glText.getData().getType().equals(GlTextPo.TYPE_DB)){
				getData().setAnswer(glText.getData().getText());
			}*/
		}
		
	}
	
}
